// 各部门管理制度
function editWdgl(id) {
  location.href = '#/editwdgl/'+id;
}
var $CertusFormAjax;
function findReguList(CertusJsonAjax,CertusFormAjax,$routeParams) {
    $CertusFormAjax= CertusFormAjax;
    // 总则
    var wdglColumns = [{
        id: 'file_type',
        title: '文件类别',
        type: 'string',
        width: 110,
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            var type = record.file_type;
            if (type=="ZZ"){
                content += '总则';
            }else if (type=="GSZD"){
                content += '公司管理制度';
            }else{
                content+="各部门管理制度";
            }
            return content;
        }
    }, {
        id: 'file_name',
        title: '文件名称',
        type: 'string',
        // width: 200,
        columnClass: 'text-center',
    }, {
        id: 'file_desc',
        title: '文件说明',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'file_version',
        title: '文件版本',
        type: 'string',
        width: 73,
        columnClass: 'text-center',
    }, {
        id: 'department_name',
        title: '维护部门',
        type: 'string',
        width: 110,
        columnClass: 'text-center',
    }, {
        id: 'user_name',
        title: '上传人员',
        type: 'string',
        width: 73,
        columnClass: 'text-center',
    }, {
        id: 'updateTime',
        title: '更新时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-success" onclick="editWdgl(' + record.id + ')"><i class="icon-cloud-upload"></i>  更新</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-info" onclick="down(\'' + record.short_name +'\')"><i class="icon-cloud-download"></i>  下载</button>';
            return content;
        }
    }];
    var code = $routeParams.code;
    if (code=="BMZD"){
        CertusJsonAjax.dataInit("findReguList",{"fileType":$routeParams.code},wdglColumns,'wdglContainerBm','wdglToolBarContainerBm');
    }else if (code=="ZZ"){
        CertusJsonAjax.dataInit("findReguList",{"fileType":$routeParams.code},wdglColumns,'wdglContainer','wdglToolBarContainer');
    }else{
        CertusJsonAjax.dataInit("findReguList",{"fileType":$routeParams.code},wdglColumns,'wdglContainerGs','wdglToolBarContainerGs');
    }
};
//文件下载权限事件
function down(shortCode) {
    if (shortCode=="") {
        //询问框
        layer.confirm('权限不足，是否申请更高权限？', {
            title: '提示信息',
            btn: ['申请', '取消'] //按钮
        }, function () {
            layer.msg('申请成功', {
                icon: 1
            });
        });
    } else {
        $CertusFormAjax.ajax("findReguByCode",{"shortCode":shortCode},function (response) {
            var content="";
            var data = response.data;
            content+='<div class="aper-layer">'
                +'<table class="table table-bordered" style="text-align: center;margin-bottom:0;">'
                +'<thead><tr><th>文件名称</th><th>版本号</th><th>上传时间</th><th>操作</th></tr></thead>'
                +'<tbody>';
            for (var i=0;i<data.length;i++){
                content+='<tr><td>'+data[i].fileName+'</td><td>'+data[i].fileVersion+'</td><td>'+data[i].createTime
                    +'</td><td><a href="javascript:download(\''+data[i].fileId+'\')">下载</a></td></tr>';
            }
            content+='</tbody></table></ul></div>';
            //页面层
            layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['480px', 'auto'], //宽高
                title: '文件下载列表',
                content: content
            });
        })
    }
}
function download(fileId) {
    if (fileId==undefined||fileId=="undefined"){
        layer.msg("文件不存在",{"time":1000});
        return;
    }
    $CertusFormAjax.download("downReguFile", "uploadFilePath=" + fileId);
}

function editRegu($scope,CertusFormAjax,$routeParams) {
    initFileUpload($scope,"","",CertusFormAjax);
    CertusFormAjax.ajax("findRegu",{"id":$routeParams.id},function (response) {
        var data = response.data;
        $scope.shortName = data.shortName;
        console.log(data.fileType)
        if (data.fileType=="ZZ"){
            $("#fileType").html("<option value='ZZ'>总则</option>");
        }else if (data.fileType=="GSZD"){
            $("#fileType").html("<option value='GSZD'>公司管理制度</option>");
        }else{
            $("#fileType").html("<option value='BMZD'>各部门管理制度</option>");
        }
        $("#fileType").val(data.fileType);
        $scope.file.fileName = data.fileName;
        $("#fileName").attr("disabled","disabled");
    });

    CertusFormAjax.ajax("companyListByViewCode",{},function (response) {
        $scope.comp = JSON.parse(response.data);
    });

    this.getDept = function () {
        CertusFormAjax.ajax("deptedList",{companyCode:$("#company").val()},function (response) {
            $scope.dept = JSON.parse(response.data);
        });
    };


    var id = $routeParams.id;
    $(".toRegu").on('click',function () {
        var form = {};
        form.compId = $scope.company;
        form.deptId = $scope.department;
        form.fileName = $("#fileName").val();
        form.fileDesc = $("#fileDesc").val();
        form.fileType = $("#fileType").val();
        form.rid = id;
        fileUpload($scope,function (res){
            form.fileId = res.data;
            CertusFormAjax.ajax("addRegu",{"param":JSON.stringify(form)}, function () {
                layer.msg("更新成功",{"time":1000});
                window.history.back();
            });
        },null,CertusFormAjax);
    })
}

//所属公司change事件
function findDept() {
    getDept();
}